<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="公司全称">
        <el-input v-model="dataForm.companyName" placeholder="公司全称搜索" clearable></el-input>
      </el-form-item>
      <el-form-item label="公司区域筛选">
        <el-popover
          ref="areaTree"
          v-model="areaTreeVisible"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="areaTree"
            :props="areaTreeProps"
            node-key="id"
            ref="goodsCatsTree"
            @current-change="areaTreeCurrentChangeHandle"
            :default-expand-all="false"
            :default-expanded-keys="[0]"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.companyAreaName"
          class="area-tree"
          v-popover:areaTree
          :readonly="true"
          placeholder="全部"
          @click="addOrUpdateVisible = true"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('merchant:company:save')"
          type="primary"
          @click="addOrUpdateHandle()"
        >新建公司</el-button>
        <el-button
          v-if="isAuth('merchant:company:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="companyName" header-align="center" align="center" label="公司全称"></el-table-column>
      <el-table-column prop="companyShortName" header-align="center" align="center" label="公司简称"></el-table-column>
      <el-table-column header-align="center" align="center" label="公司地址">
        <template slot-scope="scope">
          <template v-if="scope.row.companyAreaName != null">
            <span>{{ scope.row.companyAreaName.replace(/\//g,"") }}</span>
            <br />
            <span v-if="scope.row.companyAddress != null">{{ scope.row.companyAddress }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="managerName" header-align="center" align="center" label="管理员"></el-table-column>
      <el-table-column prop="managerMobile" header-align="center" align="center" label="管理员电话"></el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="登录账号"></el-table-column>
      <el-table-column header-align="center" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.createTime,'YYYY/MM/DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="maxShopNum" header-align="center" align="center" label="最大店铺数量">
        <template slot-scope="{row}">
          <span v-if="row.maxShopNum < 0">无限制</span>
          <span v-else>{{row.maxShopNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="maxEmployeeNum" header-align="center" align="center" label="店铺最大员工数量">
        <template slot-scope="{row}">
          <span v-if="row.maxEmployeeNum < 0">无限制</span>
          <span v-else>{{row.maxEmployeeNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="shopNum" header-align="center" align="center" label="店铺数量"></el-table-column>
      <el-table-column header-align="center" align="center" label="状态" width="170">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            active-text="已启用"
            inactive-text="已禁用"
            @change="updateStatus($event,scope.row,scope.$index)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('merchant:company:update')"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
          >修改</el-button>
          <el-button
            v-if="isAuth('merchant:company:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
          >删除</el-button>
          <el-button
            v-if="isAuth('merchant:company:shop:employee:num')"
            type="text"
            size="small"
            @click="companyNumHandle(scope.row)"
          >调整店铺、员工最大数量</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      :areaTree="areaTree"
      @refreshDataList="getDataList"
    ></add-or-update>
    <CompanyNum v-if="companyNumVisible" ref="companyNum" @refreshDataList="getDataList"></CompanyNum>
  </div>
</template>

<script>
import { getCompanys, treeDataTranslate } from "@/utils";
import AddOrUpdate from "./company-add-or-update";
import CompanyNum from "./company-num";
export default {
  data() {
    return {
      areaTreeVisible: false,
      dataForm: {
        companyName: "",
        companyAreaCode: null,
        companyAreaName: ""
      },
      // 区域树
      areaTree: [],
      areaTreeProps: {
        label: "name",
        children: "children"
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      companyNumVisible: false
    };
  },
  components: {
    AddOrUpdate,
    CompanyNum
  },
  mounted() {
    this.getAreaTree();
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/merchant/company/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          companyName: this.dataForm.companyName,
          companyAreaCode: this.dataForm.companyAreaCode
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 调整公司店铺、员工最大数量
    companyNumHandle(company) {
      this.companyNumVisible = true;
      this.$nextTick(() => {
        this.$refs.companyNum.init(company);
      });
    },
    // 修改公司状态
    updateStatus(e, row, index) {
      this.$http({
        url: this.$http.adornUrl(`/merchant/company/updateStatus`),
        method: "post",
        data: this.$http.adornData({
          id: row.id,
          status: e,
          username: row.username
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        } else {
          this.$message.error(data.msg);
          // 恢复原来的状态
          this.dataList[index].status = row.status === 1 ? 0 : 1;
        }
      });
    },
    // 区域树选中
    areaTreeCurrentChangeHandle(data, node) {
      this.dataForm.companyAreaCode = data.areaAllId;
      this.dataForm.companyAreaName = data.name;
      this.areaTreeVisible = false;
      this.getDataList();
    },
    // 获取区域树
    getAreaTree() {
      var areaTree = sessionStorage.getItem("area_tree");
      if (areaTree != undefined && areaTree.length > 0) {
        this.areaTree = JSON.parse(areaTree);
        return;
      }
      this.$http({
        url: this.$http.adornUrl("/sys/area/tree"),
        method: "get",
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.areaTree = treeDataTranslate(data.data);
          sessionStorage.setItem(
            "area_tree",
            JSON.stringify(this.areaTree || "[]")
          );
        }
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm("真的要删除所选项么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/merchant/company/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
<style lang="scss">
.el-popover {
  max-height: 300px;
  overflow: auto;
}
</style>
<style scoped>
/* .area-tree {
    >>> .el-input__inner {
      white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
    }
  } */
.mod-config .area-tree >>> .el-input__inner {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
